package com.example.distributelock.service;

import com.example.distributelock.lock.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author caryguan
 * @version V1.0
 * @Package com.example.distributelock.service
 * @date 2020/10/8 22:35
 */
@Slf4j
@Service
public class SchedulerService {

    @Autowired
    private RedisTemplate redisTemplate;

    //五秒一次的sms定时任务 cron表达式一定要写正确,不然会启动不成功,* 后面不要漏掉空格
    @Scheduled(cron = "0/5 * * * * ?")
    public void sendSms(){
        try(RedisLock redisLock=new RedisLock(redisTemplate,"autoSms",30)){
            if(redisLock.getLock()){
                log.info("向138**** 发送短信........");
            }
        }catch (Exception e){

        }
    }
}